﻿using FangChanCrm.Services.Interface;
using FangChanCrm.Services.Realization;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FangChanCrm.Controllers
{
    public class FangYuanController : Controller
    {
        private readonly It_FangYuanBiaoService t_FangYuanBiaoService;
        private readonly It_ZhanQuXiaoQuGuanLianBiaoService t_ZhanQuXiaoQuGuanLianBiaoService;

        public FangYuanController(It_FangYuanBiaoService fangYuanBiaoService, It_ZhanQuXiaoQuGuanLianBiaoService zhanQuXiaoQuGuanLianBiaoService, IConfiguration configuration)
        {
            t_FangYuanBiaoService = fangYuanBiaoService;
            t_ZhanQuXiaoQuGuanLianBiaoService = zhanQuXiaoQuGuanLianBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public IActionResult FangYuanList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult FangYuanAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult FangYuanUpdate()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
         
        public string FangYuanTianJia() 
        {
            string xiaoqumingcheng = Request.Form["xiaoqumingcheng"];

            t_FangYuanBiao fangyuan = new t_FangYuanBiao();
            fangyuan.XiaoQuMingCheng = xiaoqumingcheng;
            fangyuan.BeiZhu1 = "启用";
            int panduan = 0;

            t_FangYuanBiao fyxinxipanduan = t_FangYuanBiaoService.GetModel(fangyuan);
            if (fyxinxipanduan == null)
            {
                panduan = t_FangYuanBiaoService.Add(fangyuan);

                if (panduan > 0)
                {
                    t_FangYuanBiao fy = t_FangYuanBiaoService.GetModel(fangyuan);
                    panduan = fy.Id;
                }
                else
                {
                    panduan = 0;
                }
                return panduan.ToString();
            }
            else 
            {
                return "chuwu";
            }
        }

        public string Getid()
        {
            string id = Request.Form["id"];
            t_FangYuanBiao fangyuan = t_FangYuanBiaoService.GetIdAsync(Convert.ToInt32(id));
            string shuju = "{\"XiaoQuMingCheng\":\"" + fangyuan.XiaoQuMingCheng + "\"}";
            return shuju;
        }

        public string Update()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var FangYuan = _context.t_FangYuanBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                FangYuan.XiaoQuMingCheng = Request.Form["xiaoqumingcheng"].ToString();
                panduan = _context.SaveChanges() > 0;
            }
            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string InitializationDataTable()
        {
            t_FangYuanBiao t_FangYuanBiao = new t_FangYuanBiao();
            return t_FangYuanBiaoService.GetListWithFenYe(t_FangYuanBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }

        public string InitializationDataTableGuanLi()
        {
            t_ZhanQuXiaoQuGuanLianBiao t_ZhanQuXiaoQuGuanLianBiao = new t_ZhanQuXiaoQuGuanLianBiao();
            t_ZhanQuXiaoQuGuanLianBiao.XiaoQuId = Convert.ToInt32(Request.Form["Id"]);
            return t_ZhanQuXiaoQuGuanLianBiaoService.GetListWithFenYe(t_ZhanQuXiaoQuGuanLianBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }

        public string FangYuanGuanLianAdd() 
        {
            string Id = Request.Form["Id"];
            string soushuzhanqu = Request.Form["soushuzhanqu"];

            t_ZhanQuXiaoQuGuanLianBiao t_ZhanQuXiaoQuGuanLianBiao = new t_ZhanQuXiaoQuGuanLianBiao();
            t_ZhanQuXiaoQuGuanLianBiao.XiaoQuId = Convert.ToInt32(Id);
            t_ZhanQuXiaoQuGuanLianBiao.ZhanQuId = Convert.ToInt32(soushuzhanqu);

            t_ZhanQuXiaoQuGuanLianBiao panduanmodel = t_ZhanQuXiaoQuGuanLianBiaoService.GetModel(t_ZhanQuXiaoQuGuanLianBiao);

            if (panduanmodel == null)
            {
                int panduan = 0;

                panduan = t_ZhanQuXiaoQuGuanLianBiaoService.Add(t_ZhanQuXiaoQuGuanLianBiao);

                if (panduan > 0)
                {
                    panduan = 1;
                }
                else
                {
                    panduan = 0;
                }
                return panduan.ToString();
            }
            else 
            {
                return "chuwu";
            }
        }

        public string FangYuanGuanLianDel()
        {
            string Id = Request.Form["Id"];

            bool panduan;
            int fanhuishuju = 0;
            panduan = t_ZhanQuXiaoQuGuanLianBiaoService.DelFangFa(Convert.ToInt32(Id));

            if (panduan)
            {
                fanhuishuju = 1;
            }
            else
            {
                fanhuishuju = 0;
            }
            return fanhuishuju.ToString();
        }
        public string ZhuangTai() 
        {
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var FangYuan = _context.t_FangYuanBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                if (FangYuan.BeiZhu1 == "停用")
                {
                    FangYuan.BeiZhu1 = "启用";
                }
                else
                {
                    FangYuan.BeiZhu1 = "停用";
                }
                _context.SaveChanges();
            }
            return "";
        }
    }
}
